-- Minimap.lua (ultra-robuste, avec tooltip + icône forcée)
local ADDON, DFFT = ...
if not DFFT or not DFFT.DB then return end  -- garde-fou: si Core pas prêt, on sort
local DB = DFFT.DB

local DEFAULT_ICON = 2065604   -- FileID demandé

local function EnsureMinimapDefaults()
  DB.minimap = DB.minimap or {}
  if DB.minimap.pos == nil then DB.minimap.pos = 220 end
  if DB.minimap.hide == nil then DB.minimap.hide = false end

  -- Forcer l’icône par défaut si une ancienne valeur traîne
  if DB.minimap.icon ~= DEFAULT_ICON then
    DB.minimap.icon = DEFAULT_ICON
  end
end

-- on s’assure tout de suite (au cas où on est chargé tôt)
pcall(EnsureMinimapDefaults)

local minimapButton, minimapIcon

local function MinimapOK()
  return Minimap and Minimap.GetWidth and not (Minimap.IsForbidden and Minimap:IsForbidden())
end

local function MinimapButton_UpdatePosition()
  if not minimapButton or not MinimapOK() then return end
  EnsureMinimapDefaults()
  local angle = math.rad(DB.minimap.pos or 220)
  local radius = ((Minimap:GetWidth() or 140) / 2) + 5
  local x = math.cos(angle) * radius
  local y = math.sin(angle) * radius
  minimapButton:ClearAllPoints()
  minimapButton:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

local function MinimapButton_OnDrag()
  if not MinimapOK() then return end
  EnsureMinimapDefaults()
  local mx, my = Minimap:GetCenter()
  if not (mx and my) then return end
  local cx, cy = GetCursorPosition()
  local scale = UIParent:GetEffectiveScale()
  cx = (cx or 0) / (scale or 1)
  cy = (cy or 0) / (scale or 1)
  local angle = math.deg(math.atan2(cy - my, cx - mx))
  DB.minimap.pos = (angle or 0) % 360
  MinimapButton_UpdatePosition()
end

local function MinimapButton_Create()
  if minimapButton or not MinimapOK() then return end

  EnsureMinimapDefaults()

  minimapButton = CreateFrame("Button", "DamageFilterFT_MinimapButton", Minimap, "BackdropTemplate")
  minimapButton:SetSize(31, 31)
  minimapButton:SetFrameStrata("MEDIUM")
  minimapButton:SetFrameLevel((Minimap:GetFrameLevel() or 0) + 5)

  local border = minimapButton:CreateTexture(nil, "OVERLAY")
  border:SetSize(53, 53)
  border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
  border:SetPoint("TOPLEFT")

  minimapIcon = minimapButton:CreateTexture(nil, "BACKGROUND")
  minimapIcon:SetTexture(DEFAULT_ICON)           -- applique l’icône demandée
  DB.minimap.icon = DEFAULT_ICON                 -- corrige la valeur DB si besoin
  minimapIcon:SetSize(20, 20)
  minimapIcon:SetPoint("CENTER", 0, 0)
  if minimapIcon.SetMask then
    minimapIcon:SetMask("Textures\\MinimapMask")
  else
    minimapIcon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
  end

  minimapButton:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
  minimapButton:RegisterForClicks("LeftButtonUp", "RightButtonUp")
  minimapButton:RegisterForDrag("LeftButton")
  minimapButton:SetMovable(true); minimapButton:SetClampedToScreen(true)

  minimapButton:SetScript("OnDragStart", function(self)
    self:SetScript("OnUpdate", MinimapButton_OnDrag)
  end)
  minimapButton:SetScript("OnDragStop", function(self)
    self:SetScript("OnUpdate", nil)
  end)

  -- Tooltip (info-bulle)
  minimapButton:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
    GameTooltip:SetText("流畅战斗文本", 1, 1, 1)
    GameTooltip:AddLine("左键点击: 切换选项", 1, 1, 1)
    GameTooltip:AddLine("右键点击: 演示爆发",   1, 1, 1)
    GameTooltip:Show()
  end)
  minimapButton:SetScript("OnLeave", function()
    GameTooltip:Hide()
  end)

  minimapButton:SetScript("OnClick", function(_, btn)
    if btn == "LeftButton" then
      if DFFT.toggleOptions then DFFT.toggleOptions() end
    else
      if DFFT.showNumber and DFFT.shortNumber and DFFT.shortNumberRounded then
        local function S(a,tier,crit,onTarget,delay)
          C_Timer.After(delay or 0, function()
            local txt = (tier=="small"  and (DB.shortSmall  and DFFT.shortNumberRounded(a) or tostring(a)))
                     or (tier=="medium" and (DB.shortMedium and DFFT.shortNumberRounded(a) or tostring(a)))
                     or DFFT.shortNumber(a)
            DFFT.showNumber(a, txt, tier, true, crit)
          end)
        end
        S(420,"small",false,true,0.00)
        S(350000,"medium",false,true,0.15)
        S(2600000,"big",true,true,0.30)
        S(12500000,"vbig",true,true,0.45)
      end
    end
  end)

  MinimapButton_UpdatePosition()
  minimapButton:Show()
end

local function MinimapButton_RefreshVisibility()
  EnsureMinimapDefaults()
  if minimapButton then
    if DB.minimap.hide then minimapButton:Hide() else minimapButton:Show() end
  end
end

-- Events
local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(_, event)
  if event == "PLAYER_LOGIN" then
    EnsureMinimapDefaults()
    MinimapButton_Create()
    MinimapButton_RefreshVisibility()
  elseif event == "PLAYER_ENTERING_WORLD" then
    EnsureMinimapDefaults()
    MinimapButton_UpdatePosition()
    MinimapButton_RefreshVisibility()
  end
end)
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("PLAYER_ENTERING_WORLD")

-- API helpers
function DFFT.SetMinimapIcon(tex)
  EnsureMinimapDefaults()
  DB.minimap.icon = tex or DEFAULT_ICON
  if minimapIcon then minimapIcon:SetTexture(DB.minimap.icon) end
end

function DFFT.SetMinimapHidden(hidden)
  EnsureMinimapDefaults()
  DB.minimap.hide = not not hidden
  MinimapButton_RefreshVisibility()
end
